package com.zhuxulin.midi.event.meta;

import java.io.IOException;
import java.io.OutputStream;

import com.zhuxulin.midi.event.MidiEvent;
import com.zhuxulin.midi.util.MidiUtil;
import com.zhuxulin.midi.util.ReadVarLen;
/**
 * FF 7F event
 * @author ZhuXulin
 *
 */
public class SequencerSpecificEvent extends MetaEvent {

	private byte[] mData;
	
	public SequencerSpecificEvent(long tick, long delta, byte[] data) {
		super(tick, delta, MetaEvent.SEQUENCER_SPECIFIC, new ReadVarLen(data.length));
		
		mData = data;
	}
	
	public void setData(byte[] data) {
		mData = data;
		mLength.setValue(mData.length);
	}
	public byte[] getData() {
		return mData;
	}

	protected int getEventSize() {
		return 1 + 1 + mLength.getByteCount() + mData.length;
	}

	@Override
	public void writeToFile(OutputStream out) throws IOException {
		super.writeToFile(out);
		
		out.write(mLength.getBytes());
		out.write(mData);
	}
	@Override
	public byte[] getCmd(){
//		cmd=new byte[]{0xF7};
		cmd=MidiUtil.addByte((byte)0x7F, mLength.getBytes());
		cmd=MidiUtil.addByte(cmd, mData);
		return cmd;
	}
	@Override
	public int compareTo(MidiEvent other) {
		
		if(mTick != other.getTick()) {
			return mTick < other.getTick() ? -1 : 1;
		}
		if(mDelta.getValue() != other.getDelta()) {
			return mDelta.getValue() < other.getDelta() ? 1 : -1;
		}
		
		if(!(other instanceof SequencerSpecificEvent)) {
			return 1;
		}
		
		SequencerSpecificEvent o = (SequencerSpecificEvent)other;

		if(MidiUtil.bytesEqual(mData, o.mData, 0, mData.length)) {
			return 0;
		}
		return 1;
	}
}
